import {view, BondViewGroup, binder, handler} from "adajs";
import NewsService from "./state.js";
import List from "./../../lists";
import Messagebox from "./../../messagebox";
import Form from "./../../form";
import Input from "./../../form/input";
import Toast from "./../../toast";
import Loading from "./../../loading";
import User from "./../../../lib/user";

@view({
    className: "newsdetail",
    template: "./template.html",
    style: "./style.scss",
    dataset: {
        service: NewsService
    }
})
class News extends BondViewGroup {
    tags() {
        return {
            list: List
        }
    }

    @binder("baoming")
    baoming() {
        this.addChild(Messagebox, {
            parameter: {
                title: "报名",
                width: "360px",
                content: [
                    {
                        type: Form, option: {
                            fields: [
                                {type: Input, name: "date", label: "选择报名时间", inputType: "date"}
                            ]
                        }
                    }
                ],
                btns: [
                    {action: "sure", name: "确定"},
                    {action: "close", name: "关闭"}
                ]
            }
        }).then(messagebox => {
            messagebox.sure = (e) => {
                messagebox.getChildAt(0).getValue().then(value => {
                    if (value.date) {
                        this.dispatchEvent('baomingdone', value.date);
                    } else {
                        this.addChild(Toast, {
                            parameter: {content: "日期不能为空"}
                        });
                    }
                })
            }
        });
    }

    @handler("baomingdone")
    baomingdone({data}) {
        this.addChild(Loading).then(loading => {
            this.context.request.post("/api/lgb/app/lgb/courseapply/insert", {
                bmsj: data,
                kcid: this.getCurrentState().id,
                yhid: User.getUserInfo().id
            }).then(info => {
                if (info.code != 0) {
                    loading.showSuccess("报名成功");
                    loading.close();
                    let t = this.getChildrenByType(Messagebox)[0];
                    if (t) {
                        t.close();
                    }
                    this.commit('refreshTable');
                } else {
                    loading.showError(info.msg);
                    loading.close();
                }
            }, () => {
                loading.showError("报名失败");
                loading.close();
            });
        });
    }
}

export default News;